package com.wego.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wego.bean.PageBean;
import com.wego.domain.entity.GameData;
import com.wego.domain.query.GameDataQuery;
import com.wego.mapper.GameDataMapper;
import com.wego.service.GameDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GameDataServiceImpl implements GameDataService {
@Autowired
private GameDataMapper gameDataMapper;

@Override
public PageBean<GameData> getPage(GameDataQuery gameDataQuery) {
    final Integer pageNum = gameDataQuery.getPageNum();
    final Integer pageSize = gameDataQuery.getPageSize();
    final Page<GameData> page = PageHelper.startPage(pageNum, pageSize);
        gameDataMapper.selectWithCondition(gameDataQuery);
        return PageBean.page2pageBean(page);
    }

    @Override
    public GameData getById(Integer id) {
        return gameDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insert(GameData gameData) {
        if (gameData == null) {
            throw new IllegalArgumentException("GameData cannot be null");
        }
        if (gameData.getName() == null) {
            throw new IllegalArgumentException("Name cannot be null");
        }

        //设置更新时间为当前时间
        gameData.setCreateTime(LocalDateTime.now());
        gameData.setUpdateTime(LocalDateTime.now());
        return gameDataMapper.insertSelective(gameData);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(List<Integer> ids) {
        return gameDataMapper.deleteByPrimaryKeyIn(ids);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public int batchInsert(List<GameData> gameDataList) {
        return gameDataMapper.batchInsert(gameDataList);
    }

    @Override
    public int update(GameData gameData) {
        if (gameData == null) {
            throw new IllegalArgumentException("GameData cannot be null");
        }
        if (gameData.getId() == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }
        return gameDataMapper.updateByPrimaryKeySelective(gameData);
    }
}
